package com.ruoyi.mall.controller;

import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.ruoyi.mall.entity.MallCategory;
import com.ruoyi.mall.entity.MallCategoryProperty;
import com.ruoyi.mall.entity.MallProperty;
import com.ruoyi.mall.service.IMallCategoryPropertyService;
import com.ruoyi.mall.service.IMallPropertyService;
import com.ruoyi.mall.vo.MallCategoryVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mall.service.IMallCategoryService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品分类Controller
 * 
 * @author ruoyi
 * @date 2020-05-19
 */
@Controller
@RequestMapping("/mall/category")
public class MallCategoryController extends BaseController
{
    private String prefix = "mall/category";

    @Autowired
    private IMallCategoryService mallCategoryService;
    @Autowired
    private IMallPropertyService mallPropertyService;
    @Autowired
    private IMallCategoryPropertyService mallCategoryPropertyService;

    @RequiresPermissions("mall:category:view")
    @GetMapping()
    public String Category()
    {
        return prefix + "/category";
    }

    /**
     * 查询商品分类列表
     */
    @RequiresPermissions("mall:category:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MallCategory mallCategory)
    {
        startPage();
        List<MallCategory> list = mallCategoryService.selectMallCategoryList(mallCategory);
        return getDataTable(list);
    }

    /**
     * 导出商品分类列表
     */
    @RequiresPermissions("mall:category:export")
    @Log(title = "商品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MallCategory mallCategory)
    {
        List<MallCategory> list = mallCategoryService.selectMallCategoryList(mallCategory);
        ExcelUtil<MallCategory> util = new ExcelUtil<MallCategory>(MallCategory.class);
        return util.exportExcel(list, "category");
    }
    /**
     * 获取所有属性
     * @return
     */
    public List<MallProperty> getProperty(){
        return mallPropertyService.selectMallPropertyList(null);
    }
    /**
     * 新增商品分类
     */
    @GetMapping("/add")
    public String add( ModelMap mmap)
    {
        mmap.put("categorys", getMallCategorysByPid(0L));
        mmap.put("propertys", getProperty());
        return prefix + "/add";
    }

    /**
     * 新增保存商品分类
     */
    @RequiresPermissions("mall:category:add")
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MallCategoryVo mallCategory)
    {
        if(checkCategoryNameIsRepeat(mallCategory)){
            return AjaxResult.error("分类名称重复");
        }
        Long pid = mallCategory.getPid();
        if(null == pid){
            mallCategory.setPid(0L);
        }
        mallCategoryService.insertMallCategory(mallCategory);
        return toAjax(insertCategoryPropertys(mallCategory));
    }

    /**
     * 判断分类名称是否重复
     * @param mallCategory
     * @return
     */
    boolean checkCategoryNameIsRepeat(MallCategoryVo mallCategory){
        List<MallCategory> mallCategories = mallCategoryService.selectMallCategoryList(mallCategory);
        if(mallCategories.isEmpty()){
            return false;
        }
        return true;
    }

    private int insertCategoryPropertys(MallCategoryVo mallCategory) {
        int i = 0;
        //添加新属性
        for (String s : mallCategory.getProperty()) {
            MallCategoryProperty mallCategoryProperty = new MallCategoryProperty();
            mallCategoryProperty.setCategoryId(mallCategory.getCategoryId());
            mallCategoryProperty.setPropertyId(Long.valueOf(s));
           i= mallCategoryPropertyService.insertMallCategoryProperty(mallCategoryProperty);
        }
        return i;
    }
    //添加新属性
    private List<MallCategory> getMallCategorysByPid(Long pid){
        return mallCategoryService.selectMallCategoryByPId(pid);
    }

    /**
     * 修改商品分类
     */
    @GetMapping("/edit/{goodCategoryId}")
    public String edit(@PathVariable("goodCategoryId") Long goodCategoryId, ModelMap mmap)
    {
        MallCategory mallCategory = mallCategoryService.selectMallCategoryById(goodCategoryId);
        mmap.put("mallCategory", mallCategory);
        mmap.put("categorys", getMallCategorysByPid(0L));
        List<MallProperty> propertyByCategoryId = mallPropertyService.getPropertyByCategoryId(mallCategory.getCategoryId());
        List<Long> collect = propertyByCategoryId.stream().map(property -> property.getPropertyId()).collect(Collectors.toList());
        mmap.put("categoryPropertys", collect);
        mmap.put("propertys", getProperty());
        return prefix + "/edit";
    }

    /**
     * 修改保存商品分类
     */
    @RequiresPermissions("mall:category:edit")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MallCategoryVo mallCategory)
    {

        //删除分类对应的属性
        mallCategoryPropertyService.deleteMallCategoryPropertyByCategoryId(mallCategory.getCategoryId());
        //添加新属性
        insertCategoryPropertys(mallCategory);

        return toAjax(mallCategoryService.updateMallCategory(mallCategory));
    }


    /**
     * 删除商品分类
     */
    @RequiresPermissions("mall:category:remove")
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(mallCategoryService.deleteMallCategoryByIds(ids));
    }
}
